/**
 * 服务器端统一配置文件
 */

// 环境检测
const isDevelopment = process.env.NODE_ENV === 'development';
const isProduction = process.env.NODE_ENV === 'production';

// 开发环境配置
const DEVELOPMENT_CONFIG = {
  // 服务器配置
  SERVER_HOST: 'localhost',
  SERVER_PORT: 8081,
  CLIENT_HOST: 'localhost',
  CLIENT_PORT: 8080,
  
  // 数据库配置（如果需要）
  DB_HOST: 'localhost',
  DB_PORT: 5432,
  
  // 文件存储配置
  UPLOAD_PATH: './data',
  MAX_FILE_SIZE: 10 * 1024 * 1024, // 10MB
  
  // 日志配置
  ENABLE_CONSOLE_LOGS: true,  // 开发环境启用日志
  ENABLE_ERROR_LOGS: true,
  ENABLE_DEBUG_LOGS: true,
  LOG_LEVEL: 'debug',
  
  // CORS配置
  CORS_ORIGINS: ['http://localhost:8080', 'http://127.0.0.1:8080'],
  
  // WebSocket配置
  WS_HEARTBEAT_INTERVAL: 30000, // 30秒
  WS_TIMEOUT: 60000, // 60秒
};

// 生产环境配置
const PRODUCTION_CONFIG = {
  // 服务器配置
  SERVER_HOST: '0.0.0.0',
  SERVER_PORT: process.env.PORT || 8081,
  CLIENT_HOST: 'your-domain.com',
  CLIENT_PORT: 443,
  
  // 数据库配置
  DB_HOST: process.env.DB_HOST || 'localhost',
  DB_PORT: process.env.DB_PORT || 5432,
  
  // 文件存储配置
  UPLOAD_PATH: process.env.UPLOAD_PATH || './data',
  MAX_FILE_SIZE: 50 * 1024 * 1024, // 50MB
  
  // 日志配置
  ENABLE_CONSOLE_LOGS: false, // 生产环境关闭console日志
  ENABLE_ERROR_LOGS: true,
  ENABLE_DEBUG_LOGS: false,
  LOG_LEVEL: 'error',
  
  // CORS配置
  CORS_ORIGINS: process.env.CORS_ORIGINS ? process.env.CORS_ORIGINS.split(',') : ['https://your-domain.com'],
  
  // WebSocket配置
  WS_HEARTBEAT_INTERVAL: 30000,
  WS_TIMEOUT: 60000,
};

// 根据环境选择配置
const CONFIG = isDevelopment ? DEVELOPMENT_CONFIG : PRODUCTION_CONFIG;

// 构建完整URL的辅助函数
const buildClientUrl = (path = '') => {
  const protocol = CONFIG.CLIENT_PORT === 443 ? 'https' : 'http';
  const port = (CONFIG.CLIENT_PORT === 80 || CONFIG.CLIENT_PORT === 443) ? '' : `:${CONFIG.CLIENT_PORT}`;
  return `${protocol}://${CONFIG.CLIENT_HOST}${port}${path}`;
};

const buildServerUrl = (path = '') => {
  const protocol = CONFIG.SERVER_PORT === 443 ? 'https' : 'http';
  const port = (CONFIG.SERVER_PORT === 80 || CONFIG.SERVER_PORT === 443) ? '' : `:${CONFIG.SERVER_PORT}`;
  return `${protocol}://${CONFIG.SERVER_HOST}${port}${path}`;
};

// 日志工具函数
const logger = {
  debug: (...args) => {
    if (CONFIG.ENABLE_DEBUG_LOGS && CONFIG.ENABLE_CONSOLE_LOGS) {
      console.log('[DEBUG]', ...args);
    }
  },
  
  info: (...args) => {
    if (CONFIG.ENABLE_CONSOLE_LOGS) {
      console.info('[INFO]', ...args);
    }
  },
  
  warn: (...args) => {
    if (CONFIG.ENABLE_CONSOLE_LOGS) {
      console.warn('[WARN]', ...args);
    }
  },
  
  error: (...args) => {
    if (CONFIG.ENABLE_ERROR_LOGS) {
      console.error('[ERROR]', ...args);
    }
  },
  
  log: (...args) => {
    if (CONFIG.ENABLE_CONSOLE_LOGS) {
      console.log(...args);
    }
  }
};

// 导出配置
module.exports = {
  CONFIG,
  isDevelopment,
  isProduction,
  buildClientUrl,
  buildServerUrl,
  logger,
  
  // 便捷访问常用配置
  SERVER_HOST: CONFIG.SERVER_HOST,
  SERVER_PORT: CONFIG.SERVER_PORT,
  CLIENT_URL: buildClientUrl(),
  SERVER_URL: buildServerUrl(),
  CORS_ORIGINS: CONFIG.CORS_ORIGINS,
  UPLOAD_PATH: CONFIG.UPLOAD_PATH,
  MAX_FILE_SIZE: CONFIG.MAX_FILE_SIZE,
  WS_HEARTBEAT_INTERVAL: CONFIG.WS_HEARTBEAT_INTERVAL,
  WS_TIMEOUT: CONFIG.WS_TIMEOUT
};